<?php namespace App\Http\Controllers\admin\product;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use Illuminate\Http\Request;
use App\Models\LifeProduct;
use Input;
use Intervention\Image\ImageManagerStatic as Image;
use Redirect;
use Session;

class ProductController extends Controller {

	/**
	 * Display a listing of the resource.
	 *
	 * @return Response
	 */
	public function index()
	{
		//
		if(Session::has('admin')){
		return view("admin.product.index")->withIsLifes(true);
	    }else{
	    	return Redirect::to('admin/dologin');
	    }
	}

	public function addProduct()
	{
		return view("admin.product.add_product")->withIsLifes(true);
	}

	public function editProduct()
	{
		return view("admin.product.edit_product")->withIsLifes(true);
	}


    

	public function productList(){

       $current = (int)Input::get("current");
		$rowCount = (int)Input::get("rowCount");
		$sort = Input::get("sort")["id"];
		$sort = $sort ? $sort : "desc";

		$keywords = Input::get('searchPhrase');

		$model = new LifeProduct;

		$count = $model->get()->count();

		if($keywords){
			$data = $model->where("name", "like", "%".$keywords."%")->skip(($current - 1)*$rowCount)->take($rowCount)->get();
		}else{
			$data = $model->skip(($current - 1)*$rowCount)->take($rowCount)->get();
		}
		return array(
			"current" => $current,
			"rowCount" => $rowCount,
			"rows" => $data,
			"total" => $count
			);
	}

    public function moveImage($image){

    	$dir = base_path() . "/public/images/product";
    	is_dir($dir) || mkdir($dir,077,true);
    	$path = base_path(). "/public/upload";
    	$temp = $path . $image;
    	$newPath = $dir . "/" . $image;
    	if(is_file($temp)){
    		rename($temp, $newPath);
    	}
    }

    public function saveProduct(){
    	$data = Input::all();
    	$img = Input::get("image_name");
    	$this->moveImage($img);
    	LifeProduct::create($data);
    	return $this->json([]);
    }

    public function updateProduct(){

         $pid = Input::get("pid");
         $data = Input::except("pid");
         $img = Input::get("image_name");
         $model = LifeProduct::where("id",$pid);
         if($model->count()){
        	$this->moveImage($img);
         	$model->update($data);
         	return $this->json([]);
         }else{
         	return;
         }

    }

    public function getEditProduct(){
    	$pid = Input::get("pid");
    	$model = LifeProduct::where("id", $pid);
    	if($model->count()){
    		$data = $model->first()->toArray();
    		return $this->json(["data" => $data]);
    	}else{
    		return;
    	}
    }



    public function deleteProduct(){

    	$pid = Input::get("pid");
    	$model = LifeProduct::where("id",$pid);
    	if($model->count()){
    		$model->delete();
    		return $this->json([]);
    	}
    }

    public function updateFinance(){

    	$finid = Input::get("finid");
    	$related_id = Financing::where("id", $finid)->pluck('related_id');
    	$type = Financing::where("id", $finid)->pluck('type');
    	switch ($type) 
    	{
    		case env("FINANCING_PROJECT");
    		      $puid = projects::where("id", $related_id)->pluck("uid");
    			break;
    		case env("FINANCING_PROJECT"):
    		      $puid = Devices::where("id", $related_id)->pluck("uid");
    			break;
    		case env("FINANCING_TECH"):
    		     $puid = Technologies::where("id", $related_id)->pluck("uid");
    			break;	
    	}
    	$uid = $this->getUid();
    	if($uid != $puid){
    		return;
    	}
    
        $financeData = Input::get("report");
        $incomeData = Input::get("lirun");
        $cashData = Input::get("liuliang");
        $liabilityData = Input::get("fuzai");
        $imgArr[]  = $financeData['canbe_report_file'];
        $imgArr[]  = $financeData['prove_report_file'];
        $imgArr[]  = $financeData['gov_agreement_file'];
        $this->moveImages($imgArr, env("OTHERS_DIR"));
        $finance = Financing::where('id', $finid);
        $finance->update($financeData);
        $finance = Financing::where("financing_id", $finid);
        $finance->update($financeData);
        $finIncome = FinancialIncoming::where("financing_id", $finid);
	    $finIncome = update($incomeData);
	    $liability = Liability::where("financing_id", $finid);
	    $liability->update($liabilityData);
	    $cashFlow = cashFlow::where("financing_id", $find);
	    $cashFlow->update($cashData);
	    return $this->json([]);

    }
	/**
	 * Show the form for creating a new resource.
	 *
	 * @return Response
	 */
	public function create()
	{
		//
	}

	/**
	 * Store a newly created resource in storage.
	 *
	 * @return Response
	 */
	public function store()
	{
		//
	}

	/**
	 * Display the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id)
	{
		//
	}

	/**
	 * Show the form for editing the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($id)
	{
		//
	}

	/**
	 * Update the specified resource in storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update($id)
	{
		//
	}

	/**
	 * Remove the specified resource from storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id)
	{
		//
	}

}
